import requests
import json

def fanyi(date):
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    content = date
    data = {
        'i': content,
        'from': 'AUTO',
        'to': 'AUTO',
        'smartresult': 'dict',
        'client': 'fanyideskweb',
        'salt': '15541032671304',
        'sign': '8efe75883f64eb8bbad87c61c41760dd',
        'ts': '1554103267130',
        'bv': '617939f69fb18f112aa988d6038ae43f',
        'doctype': 'json',
        'version': '2.1',
        'keyfrom': 'fanyi.web',
        'action': 'FY_BY_REALTlME',
        'typoResult': 'false',
    }
    res = requests.post(url,data)
    str_json = res.content.decode("utf-8")
    myjson = json.loads(str_json)
    # print(myjson)
    print(content + ": %s" %(myjson['translateResult'][0][0]['tgt']))

if __name__ == '__main__':
    while True:
        keyword = input('请输入要翻译词:')
        if keyword == 'q':
            break
        fanyi(keyword)
